package com.xy.novelmanagement.controller;

import com.xy.model.Novel;
import com.xy.model.RestfulResult;
import com.xy.novelmanagement.service.IAutoPlayManagementService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/autoPlay")
public class AutoPlayManagementController {
		@Autowired
		private IAutoPlayManagementService autoPlayManagementService;

		@GetMapping("/")
		public RestfulResult getAutotPlayList() {
				List<Novel> list = autoPlayManagementService.getAutoPlayList();
				log.info("AutoPlayManagementController getAutoPlayList list : " + list);
				return RestfulResult.success(list);
		}

		@PutMapping(value = "/all/{locations}", produces = "application/json;charset=UTF-8")
		public RestfulResult setAllAutoPlay(@RequestBody List<Novel> replaceNames, @PathVariable String locations) {
				log.info("AutoPlayManagementController setAllAutoPlay replaceNames : " + replaceNames);
				log.info("AutoPlayManagementController setAllAutoPlay locations : " + locations);

				String[] tips = locations.split(",");
				Integer result = autoPlayManagementService.setAllAutoPlay(tips, replaceNames);
				log.info("AutoPlayManagementController setAllAutoPlay  result : " + result);
				if (result == 200) {
						return RestfulResult.success(result);
				} else if (result == 400) {
						return RestfulResult.failed("400", "");
				} else {
						return RestfulResult.failed("-403", "");
				}
		}


		@PutMapping(value = "/{location}", produces = "application/json;charset=UTF-8")
		public RestfulResult setAutoPlay(@RequestBody Novel novel, @PathVariable String location) {
				log.info("AutoPlayManagementController setAutoPlay novel : " + novel);
				log.info("AutoPlayManagementContrsoller setAutoPlay location : " + location);
				Integer result = autoPlayManagementService.setOneAutoPlay(location, novel);
				log.info("AutoPlayManagementController setAllAutoPlay  result : " + result);
				if (result == 200) {
						return RestfulResult.success(result);
				} else if (result == 400) {
						return RestfulResult.failed("400", "");
				} else {
						return RestfulResult.failed("-403", "");
				}
		}
}
